package mytask.dao;

import java.util.List;
import mytask.model.User;
import org.springframework.stereotype.Repository;

@Repository(value = "userDAO")
public class UserDAOImpl extends GeneralDAO implements UserDAO {

	@Override
	@SuppressWarnings("unchecked")
	public List<User> listUsers() {
		return getHibernateTemplate().find("from User");
	}

	@Override
	public void saveUser(User user) {
		getHibernateTemplate().saveOrUpdate(user);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public User getUserByEmail(String email) {
		List<User> users = getHibernateTemplate().find("from User c where c.email=?", email);
		if(users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}
	
	@Override
	public User getUserByCredentials(String email, String password) {
		User user = getUserByEmail(email);
		if(user != null) {
			if(user.getPassword().equals(password)) {
				return user;
			}
		}
		return null;
	}


}